package com.baemusic.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.stereotype.Component;

import com.baemusic.common.DataSourceDao;
import com.baemusic.domain.Area;
import com.rollen.sqlbuilder.creator.InsertCreator;

@Component
public class AreaDao extends DataSourceDao {

	public void add(Area area) {
		Connection con = null;
		try {
			con = dataSource.getConnection();

			Integer id = queryRunner.querywork(con, false,
					"select id from area where code=?",
					new ScalarHandler<Integer>(), area.getCode());
			if (id == null) {
				InsertCreator creator = new InsertCreator("area");
				creator.setValue("code", area.getCode());
				creator.setValue("name", area.getName());
				creator.setValue("pcode", area.getPcode());
				creator.setValue("levels", area.getLevels());
				PreparedStatement p = creator.createPreparedStatement(con);
				int code = p.executeUpdate();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	public Area findByName(String name) {
		Area resutl=null;
		Connection con = null;
		try {
			con = dataSource.getConnection();

			resutl= queryRunner.querywork(con, false,
					"select * from area where name=? and levels=3",
					new BeanHandler<Area>(Area.class), name);

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return resutl;

	}
}
